package com.fyzk.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.lottery.togetherbuy.HM_Popupwindow;
import com.fyzk.bean.ChaseListItem;
import com.fyzk.bean.ChaseListResponse;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.fyzk.utils.LotteryIDUtils;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import http.Common.CommandUtil;
import http.StringResultBean;

@ContentView(R.layout.dj_usercenter_zhuihao)
public class UserCenterZhuihaoActivity extends RecordBaseActivity implements
        onTopTitleClickListener {

    @ViewInject(R.id.user_zhuihao_topTitle)
    private TopTitleView topView;
    @ViewInject(R.id.user_zhuihao_content)
    private ZhuihaoRecordListView zhuiRecordView;

    private HM_Popupwindow lettery_popupView;
    private HM_Popupwindow time_popupView;
    private HM_Popupwindow status_popupView;

    @ViewInject(R.id.chasebet_lettery)
    private TextView chasebet_lettery;
    @ViewInject(R.id.chasebet_status)
    private TextView chasebet_status;
    @ViewInject(R.id.chasebet_time)
    private TextView chasebet_time;

    private String[] lottery_popup_items = {"全部彩种", "双色球", "大乐透",
            "福彩3D", "排列三", "排列五",
            "七星彩", "七乐彩", "时时彩",
            "山东11选5", "广东11选5","新疆11选5",
            "江苏快三", "安徽快三", "广西快三",
            "吉林快三", "重庆快乐十分", "广东快乐十分",
            "快乐扑克"};
    private int[] lottery_values = {-1, LotteryIDUtils.SSQ_LOTTERY_ID_INT, LotteryIDUtils.DLT_LOTTERY_ID_INT,
            LotteryIDUtils.FC3D_LOTTERY_ID_INT, LotteryIDUtils.PL3_LOTTERY_ID_INT, LotteryIDUtils.PL5_LOTTERY_ID_INT,
            LotteryIDUtils.QXC_LOTTERY_ID_INT, LotteryIDUtils.QLC_LOTTERY_ID_INT, LotteryIDUtils.CQSSC_LOTTERY_ID_INT,
            LotteryIDUtils.SD11XUAN5_LOTTERY_ID_INT, LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT, LotteryIDUtils.XJ11XUAN5_LOTTERY_ID_INT,
            LotteryIDUtils.JSK3_LOTTERY_ID_INT,	LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT,  LotteryIDUtils.GXK3_LOTTERY_ID_INT,
            LotteryIDUtils.JLK3_LOTTERY_ID_INT, LotteryIDUtils.KLSF_LOTTERY_ID_INT,	LotteryIDUtils.GDKLSF_LOTTERY_ID_INT,
            LotteryIDUtils.KLPK_LOTTERY_ID_INT};

    private String[] status_popup_items = {"全部状态", "进行中", "已追完", "已取消"};//1:进行中 2:取消 3:结束 -1:全部
    private String[] status_values = {"-1", "1", "3", "2"};
    private String[] time_popup_items = {"最近一月", "最近一周", "最近三月", "全部时间"};
    private String[] time_values = {"3", "2", "4", "1"};//1全部 2一周 3一个月 4三个月

    int lottery_value = -1;
    String status_value = "-1";
    String time_value = "3";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        net();
    }

    @Override
    protected void init() {
        // TODO Auto-generated method stub
        super.init();
        topView.setCenterText("" + getString(R.string.usercenter_zhuihao));
        zhuiRecordView.setOnCancelClickListener(new ZhuiHaoListAdapter.OnCancelClickListener() {
            @Override
            public void onCancel(String orderid) {
                requestChaseCancel(orderid);
            }
        });
        zhuiRecordView.setMoreInterface(this);
        zhuiRecordView.setUpdateInterface(this);
        zhuiRecordView.setRetryInterface(this);
        topView.setOnTitleClickListener(this);
        chasebet_lettery.setOnClickListener(this);
        chasebet_status.setOnClickListener(this);
        chasebet_time.setOnClickListener(this);

        lettery_popupView = new HM_Popupwindow(this, AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT,
                lottery_popup_items, false);
        status_popupView = new HM_Popupwindow(this, AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT,
                status_popup_items, false);
        time_popupView = new HM_Popupwindow(this, AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT,
                time_popup_items, false);

        lettery_popupView.setHm_PopupOnItemListener(new HM_Popupwindow.HM_PopuponItemListener() {
            @Override
            public void onItem(int position) {
                lottery_value = lottery_values[position];
                lettery_popupView.dismiss();
                chasebet_lettery.setText(lottery_popup_items[position]);
                zhuiRecordView.netProgressView.waiting();
                requestChaseList();
            }

            @Override
            public void popupShow() {

            }

            @Override
            public void popupDismiss() {

            }
        });

        time_popupView.setHm_PopupOnItemListener(new HM_Popupwindow.HM_PopuponItemListener() {
            @Override
            public void onItem(int position) {
                time_value = time_values[position];
                time_popupView.dismiss();
                chasebet_time.setText(time_popup_items[position]);
                zhuiRecordView.netProgressView.waiting();
                requestChaseList();
            }

            @Override
            public void popupShow() {

            }

            @Override
            public void popupDismiss() {

            }
        });

        status_popupView.setHm_PopupOnItemListener(new HM_Popupwindow.HM_PopuponItemListener() {
            @Override
            public void onItem(int position) {
                status_value = status_values[position];
                status_popupView.dismiss();
                chasebet_status.setText(status_popup_items[position]);
                zhuiRecordView.netProgressView.waiting();
                requestChaseList();
            }

            @Override
            public void popupShow() {

            }

            @Override
            public void popupDismiss() {

            }
        });

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.chasebet_lettery:
                lettery_popupView.showPopupWindow(chasebet_lettery);
                status_popupView.dismiss();
                time_popupView.dismiss();
                break;
            case R.id.chasebet_time:
                time_popupView.showPopupWindow(chasebet_time);
                lettery_popupView.dismiss();
                status_popupView.dismiss();
                break;
            case R.id.chasebet_status:
                status_popupView.showPopupWindow(chasebet_status);
                lettery_popupView.dismiss();
                time_popupView.dismiss();
                break;
        }
    }

    private void requestChaseList() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("state", status_value);//追号状态 1:进行中 2:取消 3:结束 -1:全部
            request_json.put("pageIndex", search);
            request_json.put("maxResult", 20);
            request_json.put("lotteryType", lottery_value);
            request_json.put("timeRange", time_value);//1全部 2一周 3一个月 4三个月
            httpPostJson(CommandUtil.CHASEBET, Commands.CHASELIST, request_json,
                    false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void requestChaseCancel(String chaseId) {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("chaseId", chaseId);
            httpPostJson(CommandUtil.CHASECANCEL, Commands.CHASECANCEL, request_json,
                    true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void net() {
        if (zhuiRecordView.infos == null || zhuiRecordView.infos.size() == 0) {
            zhuiRecordView.showLoading();
        }
        requestChaseList();
    }

    @Override
    public void reTry() {
        // TODO Auto-generated method stub
        net();
    }


    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.CHASEBET:
                ChaseListResponse response = JSON.parseObject(resultBean.getData(), ChaseListResponse.class);
                if (search == 1) {
                    if(response!=null && response.getChases().size() == 0){
                        zhuiRecordView.netProgressView.noneRecord(resultBean.getMessage());
                    }else{
                        zhuiRecordView.initRecordList((ArrayList<ChaseListItem>) response.getChases());
                    }
                } else {
                    zhuiRecordView.addRecordInfos((ArrayList<ChaseListItem>) response.getChases());
                }
                if (isUpdate) {
                    zhuiRecordView.listViewExt.onRefreshComplete();
                }
                break;
            case CommandUtil.CHASECANCEL:
                showToast(resultBean.getMsg());
                requestChaseList();
                break;
        }
    }


    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
        if (errorCode.equals("900003")) {
            zhuiRecordView.netProgressView.noneRecord(message);
        } else {
            zhuiRecordView.netProgressView.fail(message);
        }
    }

    @Override
    public void more() {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        search++;
        net();
    }

    @Override
    public void update() {
        isUpdate = true;
        // TODO Auto-generated method stub
        search = 1;
        net();
    }
}
